<template>
    <div class="service-focus-container">
        <h2 class="title">服务关注</h2>

        <!-- 查询表单 -->
        <el-form :inline="true" :model="searchForm" class="search-form">
            <el-form-item label="客户名:">
                <el-input v-model="searchForm.name" placeholder="请输入客户名" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleSearch">查询</el-button>
                <el-button @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 数据表格 -->
        <el-table :data="tableData" v-loading="loading" border style="width: 100%; background-color: #f5f7fa;"
            :header-cell-style="{ background: '#f9fafc', color: '#333', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }">
            <!-- <el-table-column type="index" label="序号" width="60" />
            <el-table-column prop="client.clientId" label="客户ID" width="100" />
            <el-table-column prop="nursingLevel.levelId" label="护理等级ID" width="120" /> -->
            <el-table-column prop="client.name" label="客户名" />
            <el-table-column prop="client.gender" label="性别" width="80" />
            <el-table-column prop="client.age" label="年龄" width="80" />
            <el-table-column prop="client.phone" label="电话" />
            <el-table-column prop="nursingLevel.levelName" label="护理级别" />
            <el-table-column label="操作" width="240">
                <template #default="scope">
                    <el-button type="primary" size="small" @click="openBuyDialog(scope.row)">购买护理项目</el-button>
                    <el-button type="success" size="small" @click="manageProject(scope.row)">管理护理项目</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页栏 -->
        <div class="pagination-bar">
            <el-pagination v-model:current-page="pagination.cur" v-model:page-size="pagination.size"
                :total="pagination.total" :page-sizes="[5, 10, 20]" layout="total, sizes, prev, pager, next, jumper"
                @current-change="handleCurrentChange" @size-change="handleSizeChange" />
        </div>
    </div>

    <purchase-project-dialog ref="purchaseDialogRef" />
    <manage-project-dialog ref="manageProjectDialogRef" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {inject} from 'vue'
const axios = inject("axios")
import { ElMessage } from 'element-plus'
import PurchaseProjectDialog from '@/components/caretaker/serviceFocus/PurchaseProjectDialog.vue'
import ManageProjectDialog from '@/components/caretaker/serviceFocus/ManageProjectDialog.vue'

const searchForm = ref({ name: '' })
const tableData = ref([])
const loading = ref(false)
const pagination = ref({
    cur: 1,
    size: 5,
    total: 0
})
const purchaseDialogRef = ref(null)
const manageProjectDialogRef = ref(null)

const query = async (page) => {
    loading.value = true
    try {
        const res = await axios.get('/clientNursingProject/pageClientAndLevel', {
            params: {
                cur: page,
                number: pagination.value.size,
                name: searchForm.value.name
            }
        })
        if (res.data.status === 200) {
            tableData.value = res.data.data || []
            pagination.value.total = res.data.total || 0
        } else {
            ElMessage.warning(res.data.msg || '查询失败')
        }
    } catch (err) {
        ElMessage.error('查询出错：' + err.message)
    } finally {
        loading.value = false
    }
}

const handleSearch = () => {
    pagination.value.cur = 1
    query(1)
}
const handleCurrentChange = (page) => {
    pagination.value.cur = page
    query(page)
}
const handleSizeChange = (size) => {
    pagination.value.size = size
    pagination.value.cur = 1
    query(1)
}

const openBuyDialog = (row) => {
    const { client, nursingLevel } = row
    purchaseDialogRef.value.open(client.clientId, client.name, nursingLevel.levelId, nursingLevel.levelName)
}

const manageProject = (row) => {
    const { client, nursingLevel } = row
    manageProjectDialogRef.value.open(client.clientId, client.name, nursingLevel.levelId, nursingLevel.levelName)
}

onMounted(() => {
    query(1)
})

const handleReset = () => {
  searchForm.value.name = ''
  pagination.value.cur = 1
  query(1)
}


</script>

<style scoped>
.service-focus-container {
    padding: 20px;
}

.title {
    font-size: 27.2px;
    font-weight: bold;
    color: #333;
    margin: 0 0 20px 0;
}

.search-form {
    margin-bottom: 20px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.pagination-bar {
    display: flex;
    justify-content: flex-end;
    margin-top: 2%;
    margin-bottom: 2%;
}
</style>
